package com.alks.common.utils.excelUtils.nestExcel.convert;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.time.LocalDateTime;

/**
 * @author: chen
 * @date: 2024/11/15
 * @description: 日期格式转化
 */
public class DateConvert implements Converter<LocalDateTime> {

    private static  final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";


    @Override
    public Class<LocalDateTime> supportJavaTypeKey() {
        return LocalDateTime.class;
    }


    @Override
    public WriteCellData<String> convertToExcelData(WriteConverterContext<LocalDateTime> context) throws Exception {
        LocalDateTime date = context.getValue();
        if (date == null) {
            return null;
        }
        return new WriteCellData<>(LocalDateTimeUtil.format(date, PATTERN_YYYY_MM_DD));
    }
}
